import { getMemberCenterInfo, GetMemberDetail } from "@/service/api/member.js"
import { GetGuideInfo } from "@/service/api/user.js"
import { Login } from "@/service/api/authorize.js"
import { GetProductDisplay } from "@/service/api/product.js"
import store from "@/store/index.js"
import storage from "@/uni-sub-base/utils/storage.js"
const appInit = {
    //获取个人信息
    getUserInfo: () => {
        return new Promise((resolve, reject) => {
            getMemberCenterInfo().then((res) => {
                if (res.Data) {
                    let obj = res.Data
                    storage.setCache("userInfo", obj)
                    //取个人详情补充到storage
                    appInit.getMemberDetail().then((member) => {
                        Object.assign(obj, member.Data)
                        storage.setCache("userInfo", obj)
                        resolve(obj)
                    })
                }
            })
        })
    },
    getGuideInfo: () => {
        return new Promise((resolve, reject) => {
            let param = {
                type: 1,
            }
            let path = "?type=1"
            GetGuideInfo(param, path).then((res) => {
                if (res.State == 1 && res.Data) {
                    storage.setCache("userInfo", res.Data)
                    resolve(res.Data)
                }
            })
        })
    },
    //获取个人详情
    getMemberDetail: () => {
        return new Promise((resolve, reject) => {
            GetMemberDetail().then((res) => {
                if (res.Data) {
                    resolve(res)
                }
            })
        })
    },
    // 获取整体显示设置
    getProductDisplay() {
        return new Promise((resolve, reject) => {
            GetProductDisplay({}).then((res) => {
                if (res.State) {
                    storage.setCache("productDisplay", res.Data)
                    resolve(res)
                }
            })
        })
    },
    // 获取整体显示设置
    getProductDisplayForCache() {
        return storage.getCache("productDisplay")
    },
    initLogin(param, obj) {
        return new Promise((resolve, reject) => {
            Login(param).then((res) => {
                if (res.State == 1 && res.Data) {
                    let token = res.Data.Token
                    let role = res.Data.PositionFunCodes
                    if (role) {
                        let roleArr = role.split(",")
                        storage.setCache("roleArr", roleArr)
                    }
                    obj.SET_AUTH(token)
                    let paramString = encodeURIComponent(JSON.stringify(param))
                    storage.setCache("appOther", paramString)
                    storage.setCache("businessCode", param.businessCode)
                    storage.setCache("token", token)
                    storage.setCache("jobPosition", res.Data.JobPosition)
                    let businessDomain = "https://" + res.Data.MobileDomain
                    storage.setCache("businessDomain", businessDomain)
                    uni.hideLoading()
                    appInit.getGuideInfo().then((res) => {
                        resolve(true)
                    })
                } else {
                    uni.hideLoading()
                    obj.easyToast(res.Msg)
                }
            })
        })
    },
}
module.exports = {
    initLogin: appInit.initLogin,
    getUserInfo: appInit.getUserInfo,
    getMemberDetail: appInit.getMemberDetail,
    getProductDisplay: appInit.getProductDisplay,
    getProductDisplayForCache: appInit.getProductDisplayForCache,
    getGuideInfo: appInit.getGuideInfo,
}
